package com.example.bpmn.controller;

import com.example.bpmn.service.ProcessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/processes")
@RequiredArgsConstructor
public class ProcessController {

    private final ProcessService processService;

    @PostMapping("/start")
    public Map<String, Object> startProcess(
            @RequestParam String processKey,
            @RequestParam String businessKey,
            @RequestBody(required = false) Map<String, Object> variables) {
        log.info("启动流程: processKey={}, businessKey={}, variables={}", 
            processKey, businessKey, variables);
            
        String processInstanceId = processService.startProcess(processKey, businessKey, variables);
        
        return Map.of(
            "code", "200",
            "data", Map.of(
                "processInstanceId", processInstanceId,
                "businessKey", businessKey,
                "startTime", System.currentTimeMillis()
            )
        );
    }

    @GetMapping("/instance/{processInstanceId}")
    public Map<String, Object> getProcessStatus(@PathVariable String processInstanceId) {
        log.info("查询流程状态: processInstanceId={}", processInstanceId);
        
        Map<String, Object> processStatus = processService.getProcessStatus(processInstanceId);
        
        return Map.of(
            "code", "200",
            "data", processStatus
        );
    }
} 